//
//  XS_CyclePagerView.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/6.
//

import UIKit
import RxSwift
import RxCocoa

class XS_QueryBannerM: XS_BaseModel {
    var id = 0
    var imgUrl = ""
    var jumpType = ""
    var jumpUrl = ""
    var name = ""
}

class XS_CyclePagerView: UIView {
    
    lazy var disposeBag = DisposeBag()
    lazy var data: [XS_QueryBannerM] = []
    var pagerView: TYCyclePagerView!
    var pageControl: TYPageControl!

    override init(frame: CGRect) {
        super.init(frame: frame)
        upView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func upView() {
        let imgV = UIImageView(/*image: UIImage(named: "Image may contain cartoon, illustration and screenshot备份")*/)
        addSubview(imgV)
        imgV.snp.makeConstraints({ $0.edges.equalToSuperview() })
        pagerView = TYCyclePagerView()
        pagerView.isInfiniteLoop = true
        pagerView.autoScrollInterval = 3
        pagerView.register(XS_CycleCell.self, forCellWithReuseIdentifier: "cell")
        pagerView.delegate = self
        pagerView.dataSource = self
        addSubview(pagerView)
        pagerView.snp.makeConstraints { $0.edges.equalToSuperview() }
        
        pageControl = TYPageControl()
        pageControl.currentPageIndicatorSize = CGSize(width: 8, height: 8)
        pageControl.pageIndicatorTintColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.4)
        pageControl.currentPageIndicatorTintColor = .white
        pagerView.addSubview(pageControl)
        pageControl.snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.bottom.equalTo(-.scale*16)
        }
    }
    
    func reloadData() {
        pageControl.numberOfPages = data.count
        pagerView.reloadData()
    }
}

extension XS_CyclePagerView: TYCyclePagerViewDelegate, TYCyclePagerViewDataSource {
    func numberOfItems(in pageView: TYCyclePagerView) -> Int {
        data.count
    }
    
    func pagerView(_ pagerView: TYCyclePagerView, cellForItemAt index: Int) -> UICollectionViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", for: index) as! XS_CycleCell
        cell.imgV.sd_setImage(with: URL(string: data[index].imgUrl))
        return cell
    }
    
    func layout(for pageView: TYCyclePagerView) -> TYCyclePagerViewLayout {
        let layout = TYCyclePagerViewLayout()
        layout.itemHorizontalCenter = true
        layout.itemSize = bounds.size
        layout.layoutType = .normal
        return layout
    }
    
    func pagerView(_ pageView: TYCyclePagerView, didScrollFrom fromIndex: Int, to toIndex: Int) {
        pageControl.currentPage = toIndex
    }
    
    func pagerView(_ pageView: TYCyclePagerView, didSelectedItemCell cell: UICollectionViewCell, at index: Int) {
        let model = data[index]
        switch model.jumpType {
        case "curriculum": // 跳课程
            let vc = XS_KeChengVC()
            vc.id = model.jumpUrl
            XS_NVC.shared.pushViewController(vc, animated: true)
        case "organization": // 跳机构
//            let vc = XS_WebPageVC("/collaborate?id=\(model.jumpUrl)")
            let vc = XS_WebPageVC(model.jumpUrl)
            vc.isShare = true
            XS_NVC.shared.pushViewController(vc, animated: true)
        case "new": // 跳资讯
//            let vc = XS_WebPageVC("/Home?id=\(model.jumpUrl)")
            let vc = XS_WebPageVC(model.jumpUrl)
            vc.isShare = true
            XS_NVC.shared.pushViewController(vc, animated: true)
        case "other": // 跳外链或其它
            let vc = XS_WebPageVC(model.jumpUrl)
            XS_NVC.shared.pushViewController(vc, animated: true)
        default: break
        }
        
    }
    
}

class XS_CycleCell: XS_BaseCCell {
    let imgV = UIImageView()
    override func upView() {
        contentView.addSubview(imgV)
        imgV.snp.makeConstraints({$0.edges.equalToSuperview()})
    }
}
